package com.small;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.small.act.ShopNewsActivity;
import com.small.act.UserCentreActivity;
import com.small.act.discount.TabDiscountActivity;
import com.small.act.ecard.ElectronicHomeActivity;
import com.small.commom.Action;
import com.small.commom.AsyncDataLoader;
import com.small.commom.AsyncDataLoader.Callback;
import com.small.commom.Common;
import com.small.model.SQParentModel;
import com.small.view.Home_listAda;

public class HomeActivity extends Activity {

	ListView list_view;
	Home_listAda ada;
	private Vibrator mVibrator01 = null;
	private LocationClient mLocClient;
	TextView mTv;
	TextView mTitle;

	int result = -1;
	Action action;
	HashMap<String, Object> resultSQMap;
	List<SQParentModel> shopSQList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		action = new Action(HomeActivity.this);
		setContentView(R.layout.activity_home_view);
		list_view = (ListView) this.findViewById(R.id.home_list);
		ada = new Home_listAda(this);
		list_view.setAdapter(ada);
		initview();
		mTv = (TextView) findViewById(R.id.tv_location);
		mTitle = (TextView) findViewById(R.id.tv_cityName);
		mLocClient = ((MyApp) getApplication()).mLocationClient;
		((MyApp) getApplication()).mTv = mTv;
		mVibrator01 = (Vibrator) getApplication().getSystemService(
				Service.VIBRATOR_SERVICE);
		((MyApp) getApplication()).mVibrator01 = mVibrator01;
		setLocationOption();
		mLocClient.start();
		// if (!mIsStart) {
		// setLocationOption();
		// mLocClient.start();
		// // mStartBtn.setText("停止");
		// mIsStart = true;
		//
		// } else {
		// mLocClient.stop();
		// mIsStart = false;
		// // mStartBtn.setText("开始");
		// }

	}

	private void initview() {
		list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				switch (arg2) {
				case 0:
					Intent idz = new Intent(HomeActivity.this,
							ElectronicHomeActivity.class);
					HomeActivity.this.startActivity(idz);
					break;
				case 1:
					Intent iYhq = new Intent(HomeActivity.this,
							TabDiscountActivity.class);
					HomeActivity.this.startActivity(iYhq);
					break;
				case 2:
					Intent newsIntent = new Intent(HomeActivity.this,
							ShopNewsActivity.class);
					HomeActivity.this.startActivity(newsIntent);
					break;
				case 3:

					HomeActivity.this.startActivity(new Intent(
							HomeActivity.this, UserCentreActivity.class));
					break;
				}
			}
		});
	}

	private void setLocationOption() {
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true); // 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setServiceName("com.baidu.location.service_v2.9");
		option.setPoiExtraInfo(true);

		option.setAddrType("all");
		option.setScanSpan(10000);// 设置定位模式，小于1秒则一次定位;大于等于1秒则定时定位

		// option.setPriority(LocationClientOption.NetWorkFirst); // 设置网络优先

		option.setPriority(LocationClientOption.GpsFirst); // 不设置，默认是gps优先
		// }

		option.setPoiNumber(10);
		option.disableCache(true);
		mLocClient.setLocOption(option);
	}

	@Override
	protected void onPause() {
		mLocClient.stop();
		super.onPause();
	}

	@Override
	protected void onResume() {
		mLocClient.start();
//		new AsyncDataLoader(asyncSQCallback).execute(); 
		super.onResume();
	}

	@Override
	public void onDestroy() {
		mLocClient.stop();
		((MyApp) getApplication()).mTv = null;
		super.onDestroy();
	}

//	private void loadSQData() {
//		new Thread() {
//			public void run() {
//				try {
//					resultSQMap = action.loadSQ("1");
//					resultSQMap = action.loadShopType("1");
//				} catch (Exception e) {
//				}
//			}
//		}.start();
//	}
	
//	
	private Callback asyncSQCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() {  
			resultSQMap = action.loadSQ("1");
			resultSQMap = action.loadShopType("1");
		}

		@Override
		public void onPrepare() {
		} 
		 
		@Override
		public void onFinish() {  
			if (Common.dbtool!=null) {
				Common.dbtool.close();	
			} 
		}
	};	
 
}